# coding:utf-8
import sys

sys.path.append('poi-3.10.1/poi-3.10.1-20140818.jar')
sys.path.append('poi-3.10.1/poi-ooxml-schemas-3.10.1-20140818.jar')
sys.path.append('poi-3.10.1/poi-scratchpad-3.10.1-20140818.jar')
sys.path.append('poi-3.10.1/poi-excelant-3.10.1-20140818.jar')
sys.path.append('poi-3.10.1/ooxml-lib/dom4j-1.6.1.jar')
sys.path.append('poi-3.10.1/ooxml-lib/stax-api.1.0.1.jar')
sys.path.append('poi-3.10.1/ooxml-lib/xmlbeans-2.6.0.jar')

from java.io import *
from org.apache.poi.ss.usermodel import *

def write_data(wsheet_name, wdata):
    excel_file = "data.xlsx"
    try:
        fi = FileInputStream(excel_file)
    except FileNotFoundException:
        print '[ERROR] file not found (1)'
        return

    WB = WorkbookFactory.create(fi)
    sheet = WB.getSheet(wsheet_name)
    if sheet == None:
        sheet = WB.createSheet(wsheet_name)
    row = sheet.getRow(0)
    if row == None:
        row = sheet.createRow(0)
    cell = row.getCell(0)
    if cell == None:
        cell = row.createCell(0)
    cell.setCellValue(wdata)
    
    try:
        fo = FileOutputStream(excel_file)
    except FileNotFoundException:
        print '[ERROR] file not found (2)'
        return 
    WB.write(fo)

if __name__ == '__main__':
    for i in range(3):
        sheet_name = "work_%d" % i
        wk_data = "data_%d" % i
        write_data(sheet_name, wk_data)

